package com.ts.content.job;

import com.ts.common.core.corethreadPool.ExecutorUtils;
import com.ts.content.constants.RedisKeys;
import com.ts.content.entity.DiscussEntity;
import com.ts.content.service.DiscussService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@EnableScheduling
@Component
@Slf4j
public class HotTopicJob {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private DiscussService discussService;



    @Scheduled(cron = "0/20 * * * * *")
    public void HotDiscussList(){
        log.debug("开始执行任务");
        ExecutorUtils.getThreadPoll().submit(()->{
            //冲数据库中查询出最近的数据
            List<DiscussEntity> discussEntities = discussService.getHotDiscussList();
            //先删除redis中的数据
            ListOperations<String, String> stringStringListOperations = redisTemplate.opsForList();
            redisTemplate.delete(RedisKeys.HOT_DISCUSS);

            //将最近的数据放入redis中
            for (int i = 0; i < discussEntities.size(); i++) {
                stringStringListOperations.rightPush(RedisKeys.HOT_DISCUSS,discussEntities.get(i).getTitle());
            }
        });
    }

}
